package com.zaofan.mobile.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Recharge;
import com.zaofan.exception.ServiceException;
import com.zaofan.mobile.bean.PayBean;
import com.zaofan.service.RechargeService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/mobile/recharge")
public class RechargeController extends BaseServlet {
	private static final long serialVersionUID = 5884947841079014459L;
	private RechargeService rechargeService = new RechargeService();

	protected void recharge(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Integer accountId = Integer.valueOf(request.getParameter("accountId"));
			BigDecimal money = new BigDecimal(request.getParameter("money"));
			PayBean payBean = rechargeService.recharge(accountId, money);
			JsonUtils.writeSuccessJson("操作成功", payBean, response);
			return;
		} catch (ServiceException e) {
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("充值失败", response);
		}
		JsonUtils.writeBeanFailJson("充值失败", response);
	}

	protected void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Integer id = Integer.valueOf(request.getParameter("id"));
			Recharge recharge = rechargeService.findById(id);
			if (recharge == null) {
				JsonUtils.writeBeanFailJson("参数错误!!", response);
				return;
			}
			JsonUtils.writeSuccessJson("查询成功!!", recharge, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("参数错误!!", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!", response);
	}

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Integer accountId = Integer.valueOf(request.getParameter("accountId"));
			List<Recharge> recharge = rechargeService.findByAccountId(accountId);
			JsonUtils.writeSuccessJson("查询成功!!", recharge, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

}
